﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HimVoiceStudioCore
{
    public class PreloadAssets
    {

        public Dictionary<string, EmoInfoNode> assetLookupTable = new Dictionary<string, EmoInfoNode>();

        public EmoInfoNode EmotionAssetRoot; 
        public EmoInfoNode SpliceAssetRoot;

        public EmoInfoNode GetByIndex(string index)
        {
            if(assetLookupTable.ContainsKey(index)) { return assetLookupTable[index]; }
            return null;
        }

        public string RootPath { get; private set; }
        public PreloadAssets(string resPath) { 
            this.RootPath = resPath;
            LoadAssets();
        }

        private void LoadAssets()
        {
            EmotionAssetRoot = Newtonsoft.Json.JsonConvert.DeserializeObject<EmoInfoNode>(File.ReadAllText(Path.Combine(RootPath,"emo.json")));
            SpliceAssetRoot = Newtonsoft.Json.JsonConvert.DeserializeObject<EmoInfoNode>(File.ReadAllText(Path.Combine(RootPath, "slice.json")));
            BuildLookupTable(EmotionAssetRoot);
            BuildLookupTable(SpliceAssetRoot);
        }

        private void BuildLookupTable(EmoInfoNode root)
        {
            if(root.isFile && root.indexName != null)
            {
                assetLookupTable.Add(root.indexName, root);
            }
            if(root.subDirs != null)
            {
                foreach (var item in root.subDirs)
                {
                    BuildLookupTable(item);
                }
            }
            if (root.files != null)
            {
                foreach (var item in root.files)
                {
                    BuildLookupTable(item);
                }
            }
        }

        public EmotionReference GetEmotionReference(string index)
        {
            var node = GetByIndex(index);
            if(node == null) { return null; }
            string npyPath = Path.Combine(RootPath, "emo", node.indexName + ".npz");
            string audioPath = Path.Combine(RootPath, "emo", node.indexName + ".mp3");
            if (!File.Exists(audioPath))
            {
                audioPath = null;
            }
            return new EmotionReference()
            {
                Title = node.displayName,
                FilePath = npyPath,
                ReferenceAudio = audioPath
            };
        }

        public SpliceAudio GetSpliceAudio(string index) {
            var node = GetByIndex(index);
            if (File.Exists(index))
            {
                return new SpliceAudio()
                {
                    Title = Path.GetFileName(index),
                    FilePath = Path.GetFullPath(index),

                };
            }
            if (node == null) { return null; }
            string filePath = Path.Combine(RootPath, "slices", node.indexName + ".wav");
            return new SpliceAudio()
            {
                Title = node.displayName,
                FilePath = filePath,
            };
        }
    }


    public class EmotionReference
    {
        public string Title;
        public string ReferenceAudio;
        public string FilePath;
    }

    public class SpliceAudio
    {
        public string Title;
        public string FilePath;
    }

    public class EmoInfoNode
    {
        public string displayName;
        public string indexName;
        public bool isFile = false;
        public List<EmoInfoNode> subDirs = new List<EmoInfoNode>();
        public List<EmoInfoNode> files = new List<EmoInfoNode>();
    }
}
